#include <stdio.h>
#include <math.h>
#include <stdlib.h>

///////////////////////////
/////  Fibonacci     //////
///////////////////////////


void ex3_4_fibonacci()
{
    int n, u_n, u_n_1 = 2, u_n_2 = 1;
    printf("type in the value of n\n");
    scanf("%d", &n);

    if(n == 1)
        printf("The fibonacci sequence at position 1 is: 1");
    else if(n == 2)
        printf("The fibonacci sequence at position 2 is: 2");
    else
        for(int i = 3; i <= n; i ++)
        {
            u_n = u_n_1 + u_n_2;
            u_n_2 = u_n_1;
            u_n_1 = u_n;
        }
    printf("The fibonacci sequence at position %d is: %d ", n, u_n);
}

int fibo(int n)
{
    if(n == 1)
        return 1;
    if(n == 2)
        return 2;
    return fibo(n - 1) + fibo(n - 2);
}

void fibo_print()
{
    for(int i = 1; i <= 20; i ++)
        printf("%d ", fibo(i));
}

///////////////////////////
/////  Complex Numbers
///////////////////////////


struct CplxCartisian
{
    double re, im;
};

struct CplxPolar
{
    double radius, ang;
};

struct Cplx
{
    char sys; // C for Cartisian and P for Polar
    double param1, param2;
};

double real_part(struct Cplx nb)
{
    if(nb.sys == 'C')
        return nb.param1;
    else
        return nb.param1 * cos(nb.param2);
}

void convert_to_polar_wrong(struct Cplx nb)
{
    if(nb.sys == 'P')
        return;
    nb.sys = 'P';
    double param1_tmp = nb.param1;
    nb.param1 = sqrt( nb.param1 * nb.param1  + nb.param2 * nb.param2 );
    nb.param2 = atan(param1_tmp / nb.param2);
}

struct Cplx convert_to_polar_right_1(struct Cplx nb)
{
    if(nb.sys == 'P')
        return nb;
    nb.sys = 'P';
    double param1_tmp = nb.param1;
    nb.param1 = sqrt( nb.param1 * nb.param1  + nb.param2 * nb.param2 );
    nb.param2 = atan(param1_tmp / nb.param2);
    return nb;
}

struct Cplx convert_to_polar_right_2(struct Cplx nb)
{
    if(nb.sys == 'P')
        return nb;
    struct Cplx nb_p;
    nb_p.sys = 'P';
    nb_p.param1 = sqrt( nb.param1 * nb.param1  + nb.param2 * nb.param2 );
    nb_p.param2 = atan(nb.param1 / nb.param2);
    return nb_p;
}

void convert_to_polar_right_3(struct Cplx* nb)
{
    if(nb->sys == 'P')
        return;
    nb->sys = 'P';
    double param1_tmp = nb->param1;
    nb->param1 = sqrt( nb->param1 * nb->param1  + nb->param2 * nb->param2 );
    nb->param2 = atan(param1_tmp / nb->param2);
}

void ex5_5()
{
    struct Cplx a;
    a.sys = 'C';
    a.param1 = 3;
    a.param2 = 4;

    printf("\nconvert_to_polar_wrong\n");
    convert_to_polar_wrong(a);
    printf("a: %c %lf %lf\n", a.sys, a.param1, a.param2);

    printf("\nconvert_to_polar_right_1\n");
    struct Cplx b = convert_to_polar_right_1(a);
    printf("a: %c %lf %lf\n", a.sys, a.param1, a.param2);
    printf("b: %c %lf %lf\n", b.sys, b.param1, b.param2);

    printf("\nconvert_to_polar_right_2\n");
    struct Cplx c = convert_to_polar_right_2(a);
    printf("a: %c %lf %lf\n", a.sys, a.param1, a.param2);
    printf("c: %c %lf %lf\n", c.sys, c.param1, c.param2);

    printf("\nconvert_to_polar_right_3\n");
    convert_to_polar_right_3(&a);
    printf("a: %c %lf %lf\n", a.sys, a.param1, a.param2);
}

